<!-- 
// Copyright 2008 Carnegie Mellon University
// Licensed under the Apache License, Version 2.0 (the "License"); 
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//  
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, 
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
 -->
 
<%@page import="edu.cmu.lti.sepia.task.setup.SetUp"%>
<%@page import="edu.cmu.lti.sepia.data.resource.ResourceManager"%>
<%@page import="edu.cmu.lti.sepia.data.db.SQL"%>

<%@page language="java" contentType="text/html; charset=UTF-8" %>
<%@page pageEncoding="UTF-8" %>

<%
  response.setHeader("progma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
request.setCharacterEncoding("UTF8");

String projectName = request.getParameter("projectName");
String adminPassword = request.getParameter("adminPassword");
String indriHome = request.getParameter("indriHome");
String hostname = request.getParameter("hostname");
String portnumber = request.getParameter("portnumber");
String username = request.getParameter("username");
String password = request.getParameter("password");

String batchResult = "";

boolean batchSucceeded = false;
boolean hostNotFound = false;
boolean accessDenied = false;

String propertyInitMessage = "";

boolean setupDone = ResourceManager.initializedAlready();

if ( projectName != null ) {
    application.setAttribute("projectName", projectName);
	session.setAttribute("projectName",projectName);//is this needed?

	propertyInitMessage = ResourceManager.setupProperties( 
	        hostname, portnumber, username, password, projectName, indriHome );
	ResourceManager.init();
  
	//Connection created here, so don't move it above.
	SQL sql = new SQL();

	batchResult = SetUp.setupProject( sql, projectName, adminPassword );
	batchSucceeded = batchResult.indexOf("Exception")==-1;
	hostNotFound = batchResult.indexOf("UnknownHostException")!=-1;
	accessDenied = batchResult.indexOf("Access denied")!=-1;
	sql.close();
}
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache">
<script type="text/javascript" src="../js/external/jquery.js"></script>
<script type="text/javascript"><!-- jQuery.noConflict(); //--></script>
<script type="text/javascript" src="../js/external/jquery-tipsy.js"></script>
<script type="text/javascript" src="../js/external/prototype.js"></script>
<!-- <script type="text/javascript" src="../js/shared.js"></script>-->
<script type="text/javascript">
function verifyAlphanumeric(formObj) {
  var dbName = formObj.value;
  // make it a boolean
  var passed = !!dbName.match(/^[a-zA-Z0-9_]+$/);
  if (!passed) {
    alert('You cannot use characters other than alphabets, numbers, and underscores.');
    formObj.focus();
    formObj.select();
  }
  return passed;
}

jQuery(document).ready(function($) {
  $('input').tipsy({gravity: 'w'});
});
</script>
<link rel="stylesheet" href="../css/common.css" type="text/css" />
<link rel="stylesheet" href="../css/external/jquery-tipsy.css" type="text/css" />
<title></title>
</head>
<body>

<%
if( setupDone ) {
%>  
  <h1>Set up is already done.</h1>
  If you would like to do a clean installation, please delete the property file at $SEPIA_HOME/sepia.properties first.
<%
} else {
%>

<!-- NAVIGATION -->
<div id="header"></div>

<h1>Setting up a SEPIA project</h1>

<form action="<%=request.getRequestURI() %>" method="post" name="createdb"
 onsubmit="return verifyAlphanumeric($('projectName'));">
<table>
<tr><td><strong>SEPIA Setup</strong></td><td></td></tr>
<tr><td>Project name: </td><td>
<input type="text" id="projectName" name="projectName" value="<%=(projectName!=null?projectName:"") %>" maxlength="20" 
 title="Name of the project (e.g. 'aclia'). alphabets, numbers, and underscores only." 
 onChange="verifyAlphanumeric($('projectName'))">
</td></tr>
<tr><td>Admin Password: </td><td>
<input type="password" name="adminPassword" value="<%=(adminPassword!=null?adminPassword:"") %>"  
 title="Create a password for the SEPIA admin account.">
</td></tr>

<tr><td><br><strong>Indri Setup</strong></td><td></td></tr>
<tr><td>Indri home path: </td><td>
<input type="text" name="indriHome" value="<%=indriHome!=null?indriHome:"" %>" 
 title="Enter absolute path to the indri home which contains bin/runquery bin/dumpindex" style="width:400px">
</td></tr>

<tr><td><br><strong>SEPIA DB Setup</strong></td><td></td></tr>
<tr><td>DB Hostname: </td><td>
<input type="text" name="hostname" value="<%=hostname!=null?hostname:"" %>" 
 title="Enter MySQL DB hostname" style="width:250px">
</td></tr>
<tr><td>DB Port Number: </td><td>
<input type="text" name="portnumber" value="<%=portnumber!=null?portnumber:"3306" %>" 
title="Enter MySQL DB port number (default:3306)" style="width:50px">
</td></tr>
<tr><td>DB Username: </td><td>
<input type="text" name="username" value="<%=username!=null?username:"" %>" 
 title="Enter MySQL username" style="width:120px">
</td></tr>
<tr><td>DB Password: </td><td>
<input type="password" name="password" value="<%=password!=null?password:"" %>"  
 title="Enter MySQL password" style="width:120px">
</td></tr>
</table>
<br><br>

<input type="submit" value="Create DB" <%=(batchSucceeded?"disabled":"") %> 
 title="Create a database using above information."><br>
</form><br>
<div <%=(projectName==null?"style=\"display:none\"":"") %>>
SQL execution log:<br>
<textarea rows="15" cols="120">
<%
if ( projectName != null ) {
	out.println( batchResult );
}

String message = "";
String realm = "";
if ( batchSucceeded ) {
	message = "<h2>Congratulations! Installation has been succeeded.</h2>"+
	          "Database for the SEPIA Project "+projectName+" has been successfully created.";
	realm = "<strong style=\"color:#ff0000\">Insert the following Realm block in your $CATALINA_HOME/conf/servers.xml and restart the servlet container.</strong><br>\n"+
             "<strong><code><pre style=\"color:#ffffff;background-color:#000000;padding:10px;\">"+
	         "&lt;Realm className=\"org.apache.catalina.realm.JDBCRealm\"\n"+
             "       connectionURL=\"jdbc:mysql://"+hostname+":"+portnumber+"/sepia_"+projectName+"_shared?user="+username+"&amp;amp;password="+password+"\"\n"+
             "       debug=\"99\" digest=\"MD5\" driverName=\"org.gjt.mm.mysql.Driver\"\n"+
             "       roleNameCol=\"role\" userCredCol=\"password\"\n"+
             "       userNameCol=\"user_name\" userRoleTable=\"user\"\n"+
             "       userTable=\"user\"/&gt;\n"+
             "</pre></code></strong>";
} else {
	message = "<strong>Failed to create databases for the SEPIA Project "+projectName+"</strong>";
	if ( hostNotFound ) {
		message += "<br>Reason: host \""+hostname+"\" has not been found.";
	} else if ( accessDenied ) {
		message += "<br>Reason: authentication failed. Make sure username and password are correct.";
	}
}
%>
</textarea><br>
<br>
<%=message%><br>
<br>
<%=propertyInitMessage%>
<br><br>
<%=realm%>

</div>
<br>
<br>

<!-- FOOTER -->

<div id="footer"></div>
 
<%
} // if already set up 
%>

</body>
</html>
